



console.log('[background.js] ********* background thread22 ')

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
  console.log('[background.js] ********* request ', request, sender, sendResponse)

  sendResponse('我已收到你的消息：'+JSON.stringify(request));


  getCurrentTabId(getTabId);

  return true;
})


function someMethod(){

  console.log('[background.js] someMethod call ')
}



/**
 * 获取当前选项卡id
 * @param callback - 获取到id后要执行的回调函数
 */
function getCurrentTabId(callback) {
  chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
      if (callback) {
          callback(tabs.length ? tabs[0].id: null);
      }
  });
}

function getTabId(tabId) {
  chrome.tabs.sendMessage(tabId, 'aaaa', function(response) {
    console.log('[background.js] ********* tabs.sendMessage ', response)
  });
}


//未定义default_popup时在background代码中添加监听
// 监听 chrome 插件icon 点击 事件
chrome.browserAction.onClicked.addListener(
  function(tab) { //tab对象表示当前（用户点击browser action的时候）处于活动状态的tab
    console.log('onClicked', tab);
});
//active: true, audible: false, autoDiscardable: true, discarded: false, favIconUrl: "", …

